<?php

/**
 * @author chenxuezhi
 * @copyright 2016
 */

namespace Email\Controller;
use Common\Controller\ApiController;
use Think\Log;

class IndexController extends ApiController {
    
    public function index()
    {
        //$address = '826089829@qq.com';
        //$address = array('183609025@qq.com','498082889@qq.com','1262173506@qq.com','1009238837@qq.com','826089829@qq.com','382516499@qq.com');
        //$address = array('498082889@qq.com','1009238837@qq.com','1262173506@qq.com','382516499@qq.com');
        $address = array('382516499@qq.com');
        $beforeDayTime = strtotime('- 1 day');
        //$today = date('Y-m-d', $beforeDayTime);
        $today = date('Y-m-d');
        $title = '微信小游戏'.$today.'报表';

        $message = '';
        $message .= $this->zjzxGame('西游咚咚咚', 'ybyq_game', $today);
        $message .= $this->zjzxGame('拯救紫霞', 'zjzx_game', $today);
        $message .= $this->pwzwGame('炮王之王', 'pwzw_game', $today);
		$message .= $this->monolyGame('搬砖大富翁','game_data.userdata', $today);
        $message .= $this->planeGame('全军出机','game_data.userdata', $today); 
        $message .= $this->sdxxlGame('十点消消乐','game_data.userdata', $today);
        $message .= $this->hexagonGame('2048消消乐','game_data.userdata', $today);
        $message .= $this->fireGame('弓箭大师', 'fire_game', $today);
        $message .= $this->miningGgame('全民赚钱', 'mining_game', $today);
        $message .= $this->pwzwtwoGame('炮王之王2', 'pwtwo_game', $today);
        $message .= $this->paolGame('2048泡泡', 'paol_game', $today);

        $this->sendMail($address,$title,$message);
    }

	// 清空缓存记录数据
    public function crontCache() {
        $fireDay = date("Y-m-d H:i:s");
        $this->getMonolyCacheData('搬砖大富翁','monolys',4,'game_data.userdata',$fireDay);
        $this->getPwzwCacheData('炮王之王','pwzws',1,'game_data.userdata',1,$fireDay);
        $this->getPwzwCacheData('炮王之王2','pwzwtwos',1,'game_data.userdata',5,$fireDay);
        $this->getPwzwCacheData('全军出击','planes',6,'game_data.userdata',0,$fireDay,'plane');
        $this->getHexagonsCacheData('十点消消乐','sdgames:count','sdgames',5,'game_data.userdata',0,$fireDay);
        $this->getHexagonsCacheData('2048消消乐','hexagons:count','hexagons',7,'game_data.userdata',0,$fireDay);   
    }

    public function getHexagonsCacheData($gameName,$gameKey,$gameCount,$gameType,$dbName,$dbId,$today) {
        $redis = REDIS('activity');
        $redis->select($dbId);
        $list = $redis->hGetAll($gameKey);
        $inserData = array();
        if(!empty($list)) {
            foreach($list as $k=>$v) {

                $arr = explode('-',$k);
                $info = json_decode($v,true);

                $data = array(
                    'share'=>intval($info['share']),
                    'share_user'=>intval($info['share_user']),
                    'invite'=>intval($info['invite']),
                    'auth_user'=>intval($info['auth_user']),
                    'tourist'=>intval($info['tourist']),
                    'play'=>intval($info['play']),
                    'video_click'=>intval($info['video_click']),
                    'video_close'=>intval($info['video_close']),
                    'source'=>$arr[0],
                    'z_index'=>$arr[1],
                    'type'=>$gameType,
                    'create_time'=>$today
                );

                // $data = json_decode($v,true);
                // $data['source'] = $k;
                // $data['type'] = $gameType;
                // $data['create_time'] = $today;

                $inserData[] = $data;
            }
        }

        $ret = M($dbName)->where(array('type'=>$gameType,'create_time'=>$today))->find();

        if(empty($ret)) {
            $res = M($dbName)->addAll($inserData);
            if($res) {
                $gameKey = $gameCount.'*';
                $delKey = $redis->keys($gameKey);
                $redis->del($delKey);
            }
        }
    }

    private function hexagonGame($gameName, $dbName, $today){

        $map['create_time'] = $today;
        $map['type'] = 7;
    //    $map['source'] = 0;
        $share = $share_user = $invite = $play = $video_click = $video_close = null;
        $ret = M($dbName)->field('*')->where($map)->select();
        foreach($ret as $k=>$v) {
           $share += $v['share'];
           $share_user += $v['share_user'];
           $invite += $v['invite'];
           //$auth_user += $v['auth_user'];
           //$tourist += $v['tourist'];
           $play += $v['play'];
           $video_click += $v['video_click'];
           $video_close += $v['video_close'];
       
        }

        // 授权用户
        $tourist = M('hexagon_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59'")->count();
        //$mysql = M('hexagon_game.users')->getLastSql();
        $auth_user = M('hexagon_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 2")->count();
        $not_auth = $tourist - $auth_user;


        // $auth_user = $ret['auth_user'];  // 授权用户
        // $tourist = $ret['tourist'];
        // $play = $ret['play']; // 分享用户
        // $share = $ret['share'];
        // $invite = $ret['invite'];
        // $share_user = $ret['share_user'];

    //    $gameCount = $ret['auth_user'] + $ret['tourist'];
    //    $tourist = M('hexagon_game.users')->where("status = 1 AND left(create_time,10) = '$today' ")->count();
    //    $auth_user = M('hexagon_game.users')->where("status = 2 AND left(create_time,10) = '$today' ")->count();

        $message = "<p>{$gameName} - 总用户：<b>{$tourist}</b>授权人数：<b>{$auth_user}</b> 游客人数：<b>{$not_auth}</b>游戏次数：<b>{$play}</b>分享次数：<b>{$share}</b> 邀请人数：<b>{$invite}</b>分享用户数(不重复)：<b>{$share_user}</b></p>";

        return $message;
    }


    // 十点消消乐
    private function sdxxlGame($gameName, $dbName, $today){

        $map['create_time'] = $today;
        $map['type'] = 5;
        $ret = M($dbName)->field('*')->where($map)->find();

        $auth_user = $ret['auth_user'];  // 授权用户
        $tourist = $ret['tourist'];  // 未授权用户
        $play = $ret['play']; // 分享用户
        $share = $ret['share'];
        $invite = $ret['invite'];
    
        // 授权用户
        // $auth_user = M('sdxxl_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 2 ")->count();
        // // 未授权用户
        // $tourist = M('sdxxl_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 1 ")->count();

        //$gameCount = $auth_user + $tourist;

        $message = "<p>{$gameName} - 总用户：<b>{$tourist}</b>授权用户：<b>{$auth_user}</b>游客用户：<b>{$tourist}</b> 游戏次数：<b>{$play}</b>分享次数：<b>{$share}</b> 邀请人数：<b>{$invite}</b></p>";

        return $message;
    }

    /**
     * @param  string $gameName 游戏名称
     * @param  string $gameKey 游戏主键
     * @param  string $gameType 游戏类型编号
     * @param  string $dbName 数据库名称
     * @param  string $dbId redis数据库编号
     * @param  string $today 当天时间
     * @param  string $cacheKey 补充缓存键
     * @return array
     */
    public function getPwzwCacheData($gameName,$gameKey,$gameType,$dbName,$dbId,$today,$cacheKey='') {

        $redis = REDIS('activity');
        $redis->select($dbId);
        $data = $redis->hGetAll($gameKey);

        $gameKeys = !empty($cacheKey) ? $cacheKey : $gameKey;
        $param = array(
            'share'    => (int)$data[$gameKeys.':share'], // 分享次数
            'invite'   => (int)$data[$gameKeys.':invite'], // 邀请次数
            'play' =>(int)$data[$gameKeys.':play'], //玩游戏次数
            'auth_user' =>(int)$data[$gameKeys.':auth_user'], //每日新增用户
            'tourist' =>(int)$data[$gameKeys.':tourist'], //游客用户
        );

        $param['type'] = $gameType;
        $param['create_time'] = $today;

        $ret = M($dbName)->where(array('type'=>$gameType,'create_time'=>$today))->find();
        if(empty($ret)) {
            $res = M($dbName)->add($param);
            if($res) {
                $redis->del($gameKey);
            }
        }
        return $ret;
        
    }

    /**
     * @param  string $gameName 游戏名称
     * @param  string $gameKey 游戏主键
     * @param  string $gameType 游戏类型编号
     * @param  string $dbName 数据库名称
     * @param  string $today 当天时间
     * @return array
     */
    public function getMonolyCacheData($gameName,$gameKey,$gameType,$dbName,$today) {
        $redis = REDIS('activity');
        $data = $redis->hGetAll($gameKey);

        $param = array(
            'share'    => (int)$data[$gameKey.':share'], // 分享次数
            'invite'   => (int)$data[$gameKey.':invite'], // 邀请次数
        );

        $start_time = strtotime(date('Y-m-d'));
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        // 授权用户
        $param['auth_user'] = M('monopoly_game.users')->where("create_time >= $start_time AND create_time <= $end_time AND status = 2 ")->count();
        // 未授权用户
        $param['tourist'] = M('monopoly_game.users')->where("create_time >= $start_time AND create_time <= $end_time AND status = 1 ")->count();

        $gameCount = $param['auth_user']; // 每日新增用户
        $userCount = $param['users']; // 新增用户
        $param['type'] = $gameType;
        $param['create_time'] = $today;

        $ret = M($dbName)->where(array('type'=>$gameType,'create_time'=>$today))->find();
        if(empty($ret)) {
            $res = M($dbName)->add($param);
            if($res) {
                $redis->del($gameKey);
            }
        }
        return $ret;
    }
	
    // 搬砖大富翁
    private function monolyGame($gameName, $dbName, $today){

        $map['create_time'] = $today;
        $map['type'] = 4;
        $ret = M($dbName)->field('*')->where($map)->find();

        // $auth_user = $ret['auth_user'];  // 授权用户
        // $tourist = $ret['tourist'];
        $play = $ret['play']; // 分享用户
        $share = $ret['share'];
        $invite = $ret['invite'];

        //$gameCount = $ret['auth_user'] + $ret['tourist'];

        $start_time = strtotime($today);
        $end_time = strtotime($today.'23:59:59');
        // 授权用户
        $auth_users = M('monopoly_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 2")->count();
        // 未授权用户
        $tourists = M('monopoly_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 1")->count();

        $gameCount = $auth_users + $tourists;

        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b>授权人数：<b>{$auth_users}</b> 游客人数：<b>{$tourists}</b> 分享次数：<b>{$share}</b> 邀请人数：<b>{$invite}</b></p>";

        return $message;
    }

    // 全军出机
    private function planeGame($gameName, $dbName, $today){

        $map['create_time'] = $today;
        $map['type'] = 6;
        $ret = M($dbName)->field('*')->where($map)->find();

        // $auth_user = $ret['auth_user'];  // 授权用户
        // $tourist = $ret['tourist'];
        $play = $ret['play']; // 分享用户
        $share = $ret['share'];
        $invite = $ret['invite'];

        //$gameCount = $ret['auth_user'] + $ret['tourist'];

        $tourist = M('plane_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 1 ")->count();

        $auth_user = M('plane_game.users')->where(" `create_time` BETWEEN '$today' AND '$today 23:59:59' AND status = 2 ")->count();

        //$gameCount = $auth_user + $tourist;

        $message = "<p>{$gameName} - 总用户：<b>{$tourist}</b>授权人数：<b>{$auth_user}</b> 游客人数：<b>{$tourist}</b>游戏次数：<b>{$play}</b> 分享次数：<b>{$share}</b> 邀请人数：<b>{$invite}</b></p>";

        return $message;
    }
    
    // 拯救紫霞
    private function zjzxGame($gameName, $dbName, $today)
    {
        //总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;
        
        //新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND userid NOT LIKE 'Xx%'";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;
        
        //游戏总次数
        $sql = "SELECT COUNT(1) AS num FROM $dbName.`c_users_game_result` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gamePlayCount = M()->query($sql);
        $gamePlayCount = isset($gamePlayCount[0]['num']) ? $gamePlayCount[0]['num'] : 0;
        
        // 分享次数
        // $sql = "SELECT COUNT(1) AS num FROM $dbName.`c_share_group_log` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        // $gameComCount = M()->query($sql);
        // $gameComCount = isset($gameComCount[0]['num']) ? $gameComCount[0]['num'] : 0;
        
        // 邀请次数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users_invite` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameInviteCount = M()->query($sql);
        $gameInviteCount = isset($gameInviteCount[0]['num']) ? $gameInviteCount[0]['num'] : 0;
        
        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 游戏次数：<b>{$gamePlayCount}</b> 邀请人数：<b>{$gameInviteCount}</b></p>";
        return $message;
    }
    
    
    
    // 炮王之王数据
    private function pwzwGame($gameName, $dbName, $today)
    {
        // 炮王总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;
        
        // 炮王新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND userid NOT LIKE 'Xx%'";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;
        
        $map['create_time'] = $today;
        $map['type'] = 1;
        $ret = M('game_data.userdata')->field('*')->where($map)->find();
        $gamePlayCount = $ret['play'];

        // 普通分享
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_share_group_log` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND type = 1";
        $gameComCount = M()->query($sql);
        $gameComCount = isset($gameComCount[0]['num']) ? $gameComCount[0]['num'] : 0;
        
        // 复活分享
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_share_group_log` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND type = 2";
        $gameReviveCount = M()->query($sql);
        $gameReviveCount = isset($gameReviveCount[0]['num']) ? $gameReviveCount[0]['num'] : 0;
        
        // 邀请次数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users_invite` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameInviteCount = M()->query($sql);
        $gameInviteCount = isset($gameInviteCount[0]['num']) ? $gameInviteCount[0]['num'] : 0;
        
        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$guestCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 游戏次数：<b>{$gamePlayCount}</b> 普通分享：<b>{$gameComCount}</b> 
        复活分享：<b>{$gameReviveCount}</b> 邀请人数：<b>{$gameInviteCount}</b></p>";
        return $message;
    }

    // 炮王之王2数据
    private function pwzwtwoGame($gameName, $dbName, $today)
    {
        // 炮王总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;

        // 炮王新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND status = 2 ";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;

        // 邀请人数
        $map['create_time'] = array('BETWEEN',"$today,$today 23:59:59");
        $gameInviteCount = M('pwtwo_game.users_invite')->field('*')->where($map)->count();
        $gameInviteCount = isset($gameInviteCount) ? $gameInviteCount : 0;

        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 邀请人数：<b>{$gameInviteCount}</b> </p>";
        return $message;
    }


    // 全民赚钱数据
    private function miningGgame($gameName, $dbName, $today)
    {
        // 全民赚钱 总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;

        // 全民赚钱 新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND status = 2 ";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;

        // 邀请人数
        $map['create_time'] = array('BETWEEN',"$today,$today 23:59:59");
        $gameInviteCount = M('mining_game.invite_log')->field('*')->where($map)->count();
        $gameInviteCount = isset($gameInviteCount) ? $gameInviteCount : 0;

        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 邀请人数：<b>{$gameInviteCount}</b> </p>";
        return $message;
    }

    // 弓箭大师数据
    private function fireGame($gameName, $dbName, $today)
    {
        // 弓箭大师 总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;

        // 弓箭大师 新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND status = 2 ";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;

        // 邀请人数
        $map['create_time'] = array('BETWEEN',"$today,$today 23:59:59");
        $gameInviteCount = M('fire_game.invite_log')->field('*')->where($map)->count();
        $gameInviteCount = isset($gameInviteCount) ? $gameInviteCount : 0;

        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 邀请人数：<b>{$gameInviteCount}</b> </p>";
        return $message;
    }

    // 2048泡泡
    private function paolGame($gameName, $dbName, $today)
    {
        // 2048泡泡总用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59'";
        $gameCount = M()->query($sql);
        $gameCount = isset($gameCount[0]['num']) ? $gameCount[0]['num'] : 0;

        // 2048泡泡新增用户数
        $sql = "SELECT COUNT(1) AS num FROM `$dbName`.`c_users` where `create_time` BETWEEN '$today 00:00:00' AND '$today 23:59:59' AND status = 2 ";
        $gameAddCount = M()->query($sql);
        $gameAddCount = isset($gameAddCount[0]['num']) ? $gameAddCount[0]['num'] : 0;

        // 邀请人数
        $map['create_time'] = array('BETWEEN',"$today,$today 23:59:59");
        $gameInviteCount = M('paol_game.invite_log')->field('*')->where($map)->count();
        $gameInviteCount = isset($gameInviteCount) ? $gameInviteCount : 0;

        $guestCount = $gameCount - $gameAddCount;
        $message = "<p>{$gameName} - 总用户：<b>{$gameCount}</b> 授权：<b>{$gameAddCount}</b> 
        游客：<b>{$guestCount}</b> 邀请人数：<b>{$gameInviteCount}</b> </p>";
        return $message;
    }

    // 发送邮件
    private function sendMail($address, $title, $message, $fromname = '')
    {
        $mail = new \Org\Util\PHPMailer();
        $mail->IsSMTP();
        $mail->CharSet = C('MAIL_CHARSET');
        if(is_array($address)){
            foreach($address as $email){
                $mail->AddAddress($email);
            }
        }else{
            $mail->AddAddress($address);
        }
        
        $mail->Body = $message;
        $mail->From = C('MAIL_ADDRESS');
        $mail->FromName = $fromname;
        $mail->Subject = $title;
        $mail->SMTPSecure = C('MAIL_SECURE');
        $mail->Host = C('MAIL_SMTP');
        $mail->Port = C('MAIL_PORT');
        $mail->SMTPAuth = C('MAIL_AUTH');
        $mail->Username = C('MAIL_LOGINNAME');
        $mail->Password = C('MAIL_PASSWORD');
        $mail->IsHTML(C('MAIL_HTML'));
        return $mail->Send(); 
    }

    // 盒子版本
    public function versions(){
        $config = M('game_data.config')->where('id=1')->getField('num');
        $json = array('ret' => $config, 'msg' => "游戏版本");
        $this->ajaxReturn($json);
    }
}